package inet.app.vod.view;

import java.lang.reflect.Field;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;

import com.inet.app.framework.util.Log;

public class CustomViewPager extends ViewPager {

    private boolean enabled;
    private FixedSpeedScroller mCustomScroller;
    private Field mScroller;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;
        mCustomScroller = new FixedSpeedScroller(getContext(), new AccelerateInterpolator());
        attachCustomScroller();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }
        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }
        return false;
    }
 
    public void setPagingEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    private void attachCustomScroller() {
    	try {
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true); 
            mScroller.set(this, mCustomScroller);
        } catch (NoSuchFieldException e) {
        	Log.e(e.toString());
        } catch (IllegalArgumentException e) {
        	Log.e(e.toString());
        } catch (IllegalAccessException e) {
        	Log.e(e.toString());
        }
    }
}